/**
 * 注册页面脚本
 */
let ajax = require('../../tool/ajax');
let userList = require('../user');
const screen = require('../component/screen');//截图
let pupConfig,page, config, user;

/**
 * 开始注册
 */
const start = async (myPupConfig, userName = 'gong0001') => {
  pupConfig = myPupConfig;
  page = pupConfig.page;
  config = pupConfig.config.common;
  //如果用户列表中没有，就使用默认密码
  user = userList[userName] || {id: userName, password: 'qq123456'};

  await openLogin();
  await fillForm();
}

/**
 * 打开登录页
 */
const openLogin = async () => {
  await page.goto(config.host);

  await page.waitFor('.logining-btn');
  await page.click('.logining-btn');
}

/**
 * 填写表单
 */
const fillForm = async () => {
  //等待登录模块初始化
  await ajax.waitForResponse(page, 'getImgValid.do');

  //输入登录账号
  await page.type('#loginName', user.id);
  //输入密码
  await page.type('#password', user.password);
  //输入验证码:后端不校验
  await page.type('#imgValidCode', '1234');
  //取消记住密码
  await page.click('#remember');

  await screen(page, '登录');

  //点击提交
  await page.click('button[type=submit]');

  //登录成功后，需要做判断用户信息
  await ajax.waitForResponse(page, 'getUserInfo.do');
}

module.exports = start;